package com.example.View.Renderers;

import java.util.HashMap;
import java.util.Map;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.R;
import com.example.View.Projection;
import com.example.models.Unit;
import com.example.util.Figure;

public class UnitRenderer extends FigureRendererBase {

    // The paint to be used for rendering game units
    private Paint normalPaint;

    private Map<String, Bitmap> iconByName;

    /**
     * Constructs a UnitRenderer
     */
    public UnitRenderer(Resources resources) {
        super(resources);
        normalPaint = new Paint();
        normalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        normalPaint.setARGB(255, 255, 255, 0);
        normalPaint.setAntiAlias(true);
        iconByName = new HashMap<String, Bitmap>();
        iconByName.put("DaVinciTank", createBitmap(R.drawable.tank));
        iconByName.put("Goblin", createBitmap(R.drawable.goblin));
        iconByName.put("Phoneix", createBitmap(R.drawable.phoenix));
        iconByName.put("Troll", createBitmap(R.drawable.troll));
    }

    /**
     * Renders a given figure
     * @param canvas The canvas on which (and using which) this figure is rendered
     * @param projection The translation between the model coordinate system and the view coordinate system.
     * @param figure The figure to be rendered.
     */
    @Override
    public void render(Canvas canvas, Projection projection, Figure figure) {
    	Unit unit = (Unit)figure;
        Bitmap bitmap = getBitmap(unit);
        if (unit.getDirection().getX() < 0) {
        	bitmap = Bitmap.createScaledBitmap(bitmap, -bitmap.getWidth(), bitmap.getHeight(), true);
        }
        canvas.drawBitmap(bitmap, figure.getX() * projection.getPixelsPerCell() - projection.getX(), figure.getY() * projection.getPixelsPerCell() - projection.getY(), normalPaint);
    }

    /**
     * Returns the bitmap to be used for a given unit
     */
    protected Bitmap getBitmap(Unit unit) {
        String name = unit.getName();
        Bitmap bitmap = iconByName.get(name);
        return (bitmap);
    }

}